package cn.magictodo.weblog.common.domain.mapper;

import cn.magictodo.weblog.common.domain.dos.StatisticsArticlePVDO;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author liu
 * @since 2023-11-27 13:28
 */
public interface StatisticsArticlePVMapper extends BaseMapper<StatisticsArticlePVDO> {


    /**
     * 对指定日期的文章 PV 访问量进行 增加
     * @param date
     * @return
     */
    default int increasePVCount(LocalDate date,Long number) {
        return update(null, Wrappers.<StatisticsArticlePVDO>lambdaUpdate()
                .setSql(String.format("pv_count = pv_count + %d", number))
                .set(StatisticsArticlePVDO::getUpdateTime, LocalDateTime.now())
                .eq(StatisticsArticlePVDO::getPvDate, date));
    }

    /**
     * 查询最近一周的文章 PV 访问量记录
     * @return
     */
    default List<StatisticsArticlePVDO> selectLatestWeekRecords() {
        return selectList(Wrappers.<StatisticsArticlePVDO>lambdaQuery()
                .le(StatisticsArticlePVDO::getPvDate, LocalDate.now()) // 小于等于当前日期
                .orderByDesc(StatisticsArticlePVDO::getPvDate)//倒序
                .last("limit 7")); // 仅查询七条
    }

    /**
     * 查询指定日期的文章 PV 访问量记录
     * @param date
     * @return
     */
    default StatisticsArticlePVDO selectRecordsByDate(LocalDate date) {
        return selectOne(Wrappers.<StatisticsArticlePVDO>lambdaQuery()
                .eq(StatisticsArticlePVDO::getPvDate, date));
    }
}
